package day10.Homework;

import java.io.*;
import java.util.Scanner;

public class Test1 {
    /*
    文本文件转码复制
    原文件(GBK)
    a 中
    61 D6 D0

    目标文件(UTF-8)
    a 中
    61 e4 b8 ad
     */

    public static void main(String[] args) throws IOException {

        System.out.println("原文件:");
        String from = new Scanner(System.in).nextLine();
        System.out.println("目标文件:");
        String to = new Scanner(System.in).nextLine();
        System.out.println("原文件字符编码:");
        String fromCharset = new Scanner(System.in).nextLine();
        System.out.println("目标文件字符编码:");
        String toCharset = new Scanner(System.in).nextLine();
        try {
            copy(from, to, fromCharset, toCharset);
            System.out.println("完成");
        } catch (Exception e) {
            System.out.println("失败");
            e.printStackTrace();
        }

    }

    private static void copy(String from, String to, String fromCharset, String toCharset) throws IOException {
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(to), toCharset);
        InputStreamReader in = new InputStreamReader(new FileInputStream(from),fromCharset );

        char[] buff = new char[8192];
        int n;
        while((n =in.read(buff)) != -1){
            out.write(buff, 0, n);
        }
        out.close();
        in.close();

    }
}
